import datetime
from hanchlogger import logger
import hanchxl


class infoGongDan(object):
    def __init__(self, time_d=datetime.datetime.now()):
        self.time_d = time_d
        self.gongDanBianHao = ""
        self.paiDanLeiXing = ""
        self.biaoTi = ""
        self.huiFangJieGuo = ""
        self.huiFangJieGuo2 = ""
        self.suQiuRen = ""
        self.gongDanNeiRong = ""
        self.banLiJieGuo = ""
        self.paiDanShiJian = ""
        self.chengBanBuMen = ""
        self.canYuBuMen = ""
        self.buKaoHeQingKuang = ""


class buMenBanLiInfo(object):
    def __init__(self, name_bu_men='', time_d=datetime.datetime.now()):
        self.time_d = time_d
        self.name = name_bu_men
        self.countBanJie = 0
        self.countKaoHe = 0
        self.countKaoHeQu = 0
        self.countKaoHeShi = 0
        self.countLianXiQu = 0
        self.countLianXiShi = 0
        self.countJieJueQu = 0
        self.countJieJueShi = 0
        self.countManYiQu = 0
        self.countManYiShi = 0
        self.countJiBenManYiQu = 0
        self.countJiBenManYiShi = 0
        self.countJianMianQu = 0
        self.countJianMianShi = 0
        self.lvLianXi = 0
        self.lvJieJue = 0
        self.lvManYi = 0
        self.lvJianMian = 0
        self.gongDanListQu = {}
        self.gongDanListShi = {}


def get_cheng_ban_Ban_li_info(sheet_source, row_start=2):
    infoChengBanBanLi ={}
    listChengBan = []
    maxRowSource = sheet_source.max_row
    colIndexCode = hanchxl.get_col_index(sheet_source, '工单编号')
    colIndexChengBan = hanchxl.get_col_index(sheet_source, '承办部门')
    colIndexCanYu = hanchxl.get_col_index(sheet_source, '参与部门')
    colIndexBiaoTi = hanchxl.get_col_index(sheet_source, '标题')
    colIndexLeiXing = hanchxl.get_col_index(sheet_source, '派单类型')
    colIndexLianXi = hanchxl.get_col_index(sheet_source, '是否联系')
    colIndexJieJue = hanchxl.get_col_index(sheet_source, '是否解决')
    colIndexManYi = hanchxl.get_col_index(sheet_source, '是否满意')
    colIndexJianMian = hanchxl.get_col_index(sheet_source, '是否见面')
    colIndexSuQiuRen = hanchxl.get_col_index(sheet_source, '来电人')
    colIndexPaiDanShiJian = hanchxl.get_col_index(sheet_source, '派单时间')
    colIndexGongDanNeiRong = hanchxl.get_col_index(sheet_source, '主要内容')
    colIndexBanLiJieGuo = hanchxl.get_col_index(sheet_source, '办理结果')
    colIndexHuiFangJieGuo = hanchxl.get_col_index(sheet_source, '回访结果')
    colIndexBuKaoHeQingKuang = hanchxl.get_col_index(sheet_source, '不考核情况')
    colIndexHuiFangZhuangTai = hanchxl.get_col_index(sheet_source, '回访状态')
    for row in range(row_start, maxRowSource + 1):
        valueCode = '{}'.format(sheet_source.cell(row, colIndexCode).value).strip()
        valueChengBan = '{}'.format(sheet_source.cell(row, colIndexChengBan).value).strip()
        valueCanYu = '{}'.format(sheet_source.cell(row, colIndexCanYu).value).strip()
        valueBiaoTi = '{}'.format(sheet_source.cell(row, colIndexBiaoTi).value).strip()
        valueLeiXing = '{}'.format(sheet_source.cell(row, colIndexLeiXing).value).strip()
        valueLianXi = '{}'.format(sheet_source.cell(row, colIndexLianXi).value).strip()
        valueJieJue = '{}'.format(sheet_source.cell(row, colIndexJieJue).value).strip()
        valueManYi = '{}'.format(sheet_source.cell(row, colIndexManYi).value).strip()
        valueJianMian = '{}'.format(sheet_source.cell(row, colIndexJianMian).value).strip()
        valueSuQiuRen = '{}'.format(sheet_source.cell(row, colIndexSuQiuRen).value).strip()
        valuePaiDanShiJian = '{}'.format(sheet_source.cell(row, colIndexPaiDanShiJian).value).strip()
        valueGongDanNeiRong = '{}'.format(sheet_source.cell(row, colIndexGongDanNeiRong).value).strip()
        valueBanLiJieGuo = '{}'.format(sheet_source.cell(row, colIndexBanLiJieGuo).value).strip()
        valueHuiFangJieGuo = '{}{}{}{}'.format(valueLianXi, valueJieJue, valueManYi, valueJianMian)
        valueHuiFangJieGuo2 = '{}\r\n{}\r\n{}\r\n{}'.format(valueLianXi, valueJieJue, valueManYi, valueJianMian)
        valueBuKaoHeQingKuang = '{}'.format(sheet_source.cell(row, colIndexBuKaoHeQingKuang).value).strip()
        valueHuiFangZhuangTai = '{}'.format(sheet_source.cell(row, colIndexHuiFangZhuangTai).value).strip()
        if valueChengBan not in listChengBan:
            listChengBan.append(valueChengBan)
            buMenBanLiInfoNew = buMenBanLiInfo(valueChengBan)
            infoChengBanBanLi[valueChengBan] = buMenBanLiInfoNew
        buMenBanLiInfoDemo = infoChengBanBanLi[valueChengBan]
        buMenBanLiInfoDemo.countBanJie = buMenBanLiInfoDemo.countBanJie + 1
        if valueHuiFangZhuangTai == "有效回访":
            infoGongDanDemo = infoGongDan()
            infoGongDanDemo.gongDanBianHao = valueCode
            infoGongDanDemo.paiDanLeiXing = valueLeiXing
            infoGongDanDemo.biaoTi = valueBiaoTi
            infoGongDanDemo.huiFangJieGuo = valueHuiFangJieGuo
            infoGongDanDemo.huiFangJieGuo2 = valueHuiFangJieGuo2
            infoGongDanDemo.suQiuRen = valueSuQiuRen
            infoGongDanDemo.gongDanNeiRong = valueGongDanNeiRong
            infoGongDanDemo.banLiJieGuo = valueBanLiJieGuo
            infoGongDanDemo.paiDanShiJian = valuePaiDanShiJian
            infoGongDanDemo.chengBanBuMen = valueChengBan
            infoGongDanDemo.canYuBuMen = valueCanYu
            infoGongDanDemo.buKaoHeQingKuang = valueBuKaoHeQingKuang
            if valueLeiXing == '市派' or valueLeiXing == '直派':
                buMenBanLiInfoDemo.countKaoHeShi = buMenBanLiInfoDemo.countKaoHeShi + 1
                buMenBanLiInfoDemo.countKaoHe = buMenBanLiInfoDemo.countKaoHe + 1
                buMenBanLiInfoDemo.gongDanListShi[valueCode] = infoGongDanDemo
                if valueLianXi == '已联系' or valueLianXi == '联系':
                    buMenBanLiInfoDemo.countLianXiShi = buMenBanLiInfoDemo.countLianXiShi + 1
                if valueJieJue == '已解决' or valueJieJue == '解决':
                    buMenBanLiInfoDemo.countJieJueShi = buMenBanLiInfoDemo.countJieJueShi + 1
                if valueManYi == '满意' or valueManYi == '非常满意':
                    buMenBanLiInfoDemo.countManYiShi = buMenBanLiInfoDemo.countManYiShi + 1
                if valueManYi == '基本满意':
                    buMenBanLiInfoDemo.countJiBenManYiShi = buMenBanLiInfoDemo.countJiBenManYiShi + 1
                if valueJianMian == '见面' or valueJianMian == '已见面':
                    buMenBanLiInfoDemo.countJianMianShi = buMenBanLiInfoDemo.countJianMianShi + 1
            else:
                buMenBanLiInfoDemo.countKaoHeQu = buMenBanLiInfoDemo.countKaoHeQu + 1
                buMenBanLiInfoDemo.countKaoHe = buMenBanLiInfoDemo.countKaoHe + 1
                buMenBanLiInfoDemo.gongDanListQu[valueCode] = infoGongDanDemo
                if valueLianXi == '已联系' or valueLianXi == '联系':
                    buMenBanLiInfoDemo.countLianXiQu = buMenBanLiInfoDemo.countLianXiQu + 1
                if valueJieJue == '已解决' or valueJieJue == '解决':
                    buMenBanLiInfoDemo.countJieJueQu = buMenBanLiInfoDemo.countJieJueQu + 1
                if valueManYi == '满意' or valueManYi == '非常满意':
                    buMenBanLiInfoDemo.countManYiQu = buMenBanLiInfoDemo.countManYiQu + 1
                if valueManYi == '基本满意':
                    buMenBanLiInfoDemo.countJiBenManYiQu = buMenBanLiInfoDemo.countJiBenManYiQu + 1
                if valueJianMian == '见面' or valueJianMian == '已见面':
                    buMenBanLiInfoDemo.countJianMianQu = buMenBanLiInfoDemo.countJianMianQu + 1
        infoChengBanBanLi[valueChengBan] = buMenBanLiInfoDemo
    for nameChengBan in listChengBan:
        demo2 = infoChengBanBanLi[nameChengBan]
        # if demo2.countKaoHeQu > 0 and demo2.countKaoHeShi > 0:
        #     demo2.lvLianXi = 100 * round(
        #         (demo2.countLianXiQu / demo2.countKaoHeQu + demo2.countLianXiShi / demo2.countKaoHeShi) / 2, 2)
        #     demo2.lvManYi = 100 * round(((demo2.countManYiQu + demo2.countJiBenManYiQu * 0.8) / demo2.countKaoHeQu + (
        #                 demo2.countManYiShi + demo2.countJiBenManYiShi * 0.8) / demo2.countKaoHeShi) / 2, 2)
        #     demo2.lvJieJue = 100 * round(
        #         (demo2.countJieJueQu / demo2.countKaoHeQu + demo2.countJieJueShi / demo2.countKaoHeShi) / 2, 2)
        #     demo2.lvJianMian = 100 * round(
        #         (demo2.countJianMianQu / demo2.countKaoHeQu + demo2.countJianMianShi / demo2.countKaoHeShi) / 2, 2)
        # elif demo2.countKaoHeQu > 0:
        #     demo2.lvLianXi = 100 * round(demo2.countLianXiQu / demo2.countKaoHeQu , 2)
        #     demo2.lvJieJue = 100 * round(demo2.countJieJueQu / demo2.countKaoHeQu, 2)
        #     demo2.lvManYi = 100 * round((demo2.countManYiQu + demo2.countJiBenManYiQu * 0.8) / demo2.countKaoHeQu, 2)
        #     demo2.lvJianMian = 100 * round(demo2.countJianMianQu / demo2.countKaoHeQu, 2)
        # elif demo2.countKaoHeShi > 0:
        #     demo2.lvLianXi = 100 * round(demo2.countLianXiShi / demo2.countKaoHeShi , 2)
        #     demo2.lvJieJue = 100 * round(demo2.countJieJueShi / demo2.countKaoHeShi, 2)
        #     demo2.lvManYi = 100 * round((demo2.countManYiShi + demo2.countJiBenManYiShi * 0.8) / demo2.countKaoHeShi, 2)
        #     demo2.lvJianMian = 100 * round(demo2.countJianMianShi / demo2.countKaoHeShi, 2)
        logger.debug('{}办结{}件，有效考核{}件（区派{}件，市派{}件）'.format(nameChengBan, demo2.countBanJie, demo2.countKaoHe,
                                                                         demo2.countKaoHeQu, demo2.countKaoHeShi))
        if demo2.countKaoHe > 0:
            demo2.lvLianXi = 100 * round((demo2.countLianXiShi + demo2.countLianXiQu) / demo2.countKaoHe, 2)
            demo2.lvJieJue = 100 * round((demo2.countJieJueShi + demo2.countJieJueQu) / demo2.countKaoHe, 2)
            demo2.lvManYi = 100 * round((demo2.countManYiShi + demo2.countManYiQu + (
                        demo2.countJiBenManYiShi + demo2.countJiBenManYiQu) * 0.8) / demo2.countKaoHe, 2)
            demo2.lvJianMian = 100 * round((demo2.countJianMianShi + demo2.countJianMianQu) / demo2.countKaoHe, 2)
        infoChengBanBanLi[nameChengBan] = demo2
    for index in infoChengBanBanLi:
        logger.debug('{}办结{}件，有效考核{}件（区派{}件，市派{}件），联系率{}，解决率{}，满意率{}，见面率{}'.format(index,
                                                                         infoChengBanBanLi[index].countBanJie,
                                                                         infoChengBanBanLi[index].countKaoHe,
                                                                         infoChengBanBanLi[index].countKaoHeQu,
                                                                         infoChengBanBanLi[index].countKaoHeShi,
                                                                         infoChengBanBanLi[index].lvLianXi,
                                                                         infoChengBanBanLi[index].lvJieJue,
                                                                         infoChengBanBanLi[index].lvManYi,
                                                                         infoChengBanBanLi[index].lvJianMian))
    return infoChengBanBanLi


